import random
import secrets
from urllib.parse import quote_plus

import requests
from requests.auth import HTTPBasicAuth

domains = (
    'homelandin.top',
    'aelira.autos',
    'seravix.uno',
    'branolia.cloud',
    'tessora.xyz',
    'sovora.asia',
    'kivonowo.asia',
    'vayomi.space',
    'jovano.pw',
    'fluvara.cyou',
    'luxantia.space',
    'plurevo.icu',
    'isomare.store',
    'zalvico.top'
)

ADMIN_PSW = 'TxFEv0zzqmObrNQ6'
EMAIL_PSW = 'shuyuan008'


def create_box(email_name=None, email_psw=EMAIL_PSW, domain=None):
    if domain is None:
        domain = random.choice(domains)

    # if email_name is None, random tokens will be generated.
    if email_name is None:
        email_name = secrets.token_urlsafe(8)
    if '@' not in email_name:
        email = f'{email_name}@{domain}'
    else:
        email = email_name

    auth = HTTPBasicAuth(f'admin@dt-vpn.top', ADMIN_PSW)
    body = {
        'name': email_name,
        'email': email,
        'passwordPlaintext': email_psw,
        'disabled': False,
        'superAdmin': False,
    }

    r = requests.post(f'https://mail.homelandin.top/admin/api/v1/boxes', auth=auth, json=body, timeout=30)
    # print(r.status_code, r.text)
    if r.status_code == 201:
        email = email.lower()
        print(f'Email {email}:{email_psw} created.')
        return email, email_psw
    else:
        print(f'Email create failed.')
    return None, None


def delete_box(email):
    domain = email.split('@')[-1]
    auth = HTTPBasicAuth(f'admin@{domain}', ADMIN_PSW)
    r = requests.delete(f'https://mail.{domain}/admin/api/v1/boxes/{quote_plus(email)}', auth=auth, timeout=30)
    # print(r.status_code, r.text)
    if r.status_code == 204:
        print(f'Email {email} deleted.')
    else:
        print(f'Email delete failed.')


import random
import string


def custom_password(length):
    # 定义包含所有可能字符的字符串
    all_chars = string.ascii_letters + string.digits
    # 生成密码
    password = ''.join(random.choice(all_chars) for _ in range(length))
    return password


if __name__ == '__main__':
    create_box("Wuzhiyin86")

